package com.frog.sqs;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.frog.canal.starter.annotation.CanalListener;
import com.frog.canal.starter.annotation.CanalUpdateListener;
import com.frog.canal.starter.model.CanalMessage;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * @author wuqiong 2022/4/16
 * @description
 */
//@Service
public class CanalLintenerTest {


    @CanalUpdateListener(destination = "duxinglangzi", database = "books", table = {"users"})
    public void oneEvent(CanalMessage canalMessage) {
//        System.out.println("--->>> rowData body " + rowData.toString());
        canalMessage.getRowData().getAfterColumnsList().stream().filter(e -> e.getUpdated()).collect(Collectors.toList()).forEach(ele -> {
            System.out.println("[oneEvent]字段名: " + ele.getName() + " ,是否修改：" + ele.getUpdated() + ", 修改后的值为: " + ele.getValue());
        });
    }

    @CanalUpdateListener(destination = "duxinglangzi", database = "books", table = {"books"})
    public void oneEventBooks(CanalMessage canalMessage) {
        canalMessage.getRowData().getAfterColumnsList().stream().filter(e -> e.getUpdated()).collect(Collectors.toList()).forEach(ele -> {
            System.out.println("[oneEventBooks]字段名: " + ele.getName() + " ,是否修改：" + ele.getUpdated() + ", 修改后的值为: " + ele.getValue());
        });
    }

    @CanalListener(destination = "duxinglangzi", database = "books", eventType = CanalEntry.EventType.UPDATE)
    public void oneEventBooksAll(CanalMessage canalMessage) {
        canalMessage.getRowData().getAfterColumnsList().stream().filter(e -> e.getUpdated()).collect(Collectors.toList()).forEach(ele -> {
            System.out.println("[oneEventBooksAll] --->> 字段名: " + ele.getName() + " ,是否修改：" + ele.getUpdated() + ", 修改后的值为: " + ele.getValue());
        });
    }

    @CanalListener(destination = "duxinglangzi", eventType = CanalEntry.EventType.UPDATE)
    public void oneEventBooksAllData(CanalMessage canalMessage) {
        canalMessage.getRowData().getAfterColumnsList().stream().filter(e -> e.getUpdated()).collect(Collectors.toList()).forEach(ele -> {
            System.out.println("[oneEventBooksAllData] --->> 字段名: " + ele.getName() + " ,是否修改：" + ele.getUpdated() + ", 修改后的值为: " + ele.getValue());
        });
    }

    @CanalListener(eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE})
    public void oneEventBooksAllDataaaa(CanalMessage canalMessage) {
        canalMessage.getRowData().getAfterColumnsList().stream().filter(e -> e.getUpdated()).collect(Collectors.toList()).forEach(ele -> {
            System.out.println("[oneEventBooksAllDataaaa] --->> 字段名: " + ele.getName() + " ,是否修改：" + ele.getUpdated() + ", 修改后的值为: " + ele.getValue());
        });

        if (canalMessage.getEventType() == CanalEntry.EventType.DELETE) {
            System.out.println( canalMessage.getRowData().getBeforeColumnsList());
        } else if (canalMessage.getEventType() == CanalEntry.EventType.INSERT) {
            System.out.println(canalMessage.getRowData().getAfterColumnsList());
        } else {
            System.out.println("-------&gt; before");
            System.out.println(canalMessage.getRowData().getBeforeColumnsList());
            System.out.println("-------&gt; after");
            System.out.println(canalMessage.getRowData().getAfterColumnsList());
        }


    }


}
